home *** CD-ROM | disk | FTP | other *** search
- Path: news.crystalball.com!news
- From: Larry Weiss <lfw@oc.com>
- Newsgroups: comp.lang.c
- Subject: Re: Determining the length of an int in string form
- Date: Tue, 19 Mar 1996 19:56:35 -0600
- Organization: crystalball.com
- Message-ID: <314F65D3.6A8@oc.com>
- References: <3146D058.DD7@cbm.com> <4i7uth$qph@inet-nntp-gw-1.us.oracle.com> <DoE38u.GIH@iquest.net> <4in1ga$ogk@airdmhor.gen.nz> <Pine.SOL.3.91.960319172332.4535A-100000@darwin.UCSC.EDU>
- NNTP-Posting-Host: external.oc.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- Hal Royaltey wrote:
- >
- > On 20 Mar 1996, Simon Hosie wrote:
- > > wkaufman@wkaufman.us.oracle.com (William Kaufman) wrote:
- > > +In article <3146D058.DD7@cbm.com> Dave Payne <paynedc@cbm.com> writes:
- > > +] I have a variable of type int, and I would like to use the sprintf()
- > > +] function to write this variable to a string. However, I want to
- > > +] dynamically allocate the space for the string, and only malloc enough
- > > +] space to hold the int.
- >
- > Try taking the base 10 log of the number, add 1, and discard the fractional
- > portion. That oughta give you the right number of characters, excluding a
- > sign character. However, I can't imagine any circumstance under which
- > taking base 10 logs is better than simply wasting a few bytes of string
- > space.
-
-
- Maybe you have a really large number of strings to allocate?
-
- Anyway, myself, I like precision and safety in knowing that the storage
- absolutely will be used properly.
-
- I've seen some suggestions to try to fprintf() once to a file (maybe a
- "bit-bucket") to get a handle on how many characters sprintf() may use,
- but I don't think that that's guaranteed by the C Standard to absolutely
- correspond.
-